package io.gitee.zkool.barcodegun.pc.utils;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * 打字机器人
 * @author zhaokk
 * @since 2024/2/24
 */
public class TypingRobot extends Robot {

    public TypingRobot() throws AWTException {
        super();
    }

    public void keyType(int keyCode) {
        keyPress(keyCode);
//        delay(50);
        keyRelease(keyCode);
    }

    public void keyType(int keyCode, int keyCodeModifier) {
        keyPress(keyCodeModifier);
        keyPress(keyCode);
//        delay(50);
        keyRelease(keyCode);
        keyRelease(keyCodeModifier);
    }

    public void type(String text) {
        //String textUpper = text.toUpperCase();
        String textUpper = text;

        for (int i=0; i<text.length(); ++i) {
            typeChar(textUpper.charAt(i));
        }
    }

    private void typeChar(char c) {
        boolean shift = true;
        int keyCode;
        switch (c) {
            case '~':
                keyCode = (int)'`';
                break;
            case '!':
                keyCode = (int)'1';
                break;
            case '@':
                keyCode = (int)'2';
                break;
            case '#':
                keyCode = (int)'3';
                break;
            case '$':
                keyCode = (int)'4';
                break;
            case '%':
                keyCode = (int)'5';
                break;
            case '^':
                keyCode = (int)'6';
                break;
            case '&':
                keyCode = (int)'7';
                break;
            case '*':
                keyCode = (int)'8';
                break;
            case '(':
                keyCode = (int)'9';
                break;
            case ')':
                keyCode = (int)'0';
                break;
            case ':':
                keyCode = (int)';';
                break;
            case '_':
                keyCode = (int)'-';
                break;
            case '+':
                keyCode = (int)'=';
                break;
            case '|':
                keyCode = (int)'\\';
                break;
            case '?':
                keyCode = (int)'/';
                break;
            case '{':
                keyCode = (int)'[';
                break;
            case '}':
                keyCode = (int)']';
                break;
            case '<':
                keyCode = (int)',';
                break;
            case '>':
                keyCode = (int)'.';
                break;
            case '/':
                keyCode = 47;
                shift = false;
                break;
            case ' ':
                keyCode = (int)' ';
                shift = false;
                break;
            case '.':
                keyCode = (int)'.';
                shift = false;
                break;
            case '\n':
                keyCode = (int)'\n';
                shift = false;
                break;
            default:
                //大写字母
               if( (int)c>=65 && (int)c<=90 ){
                    keyCode = (int)c;
                }else if( (int)c>=97 && (int)c<=122 ){ //小写字母
                    keyCode = (int)c-32;
                    shift = false;
                }else{
                    keyCode = (int)c;
                    shift = false;
                }
        }
        if (shift) {
            keyType(keyCode, KeyEvent.VK_SHIFT);
        } else {
            keyType(keyCode);
        }
    }

    public static void main(String[] args) {
        try {
            Thread.sleep(2*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        TypingRobot robot = null;
        try {
            robot = new TypingRobot();
            robot.type("？aA123://??");
        } catch (AWTException e) {
            e.printStackTrace();
        }

    }

}
